#include "MephistoCAM.h"

#define REG_NUM 16

int main( int argc, char *argv[] ){

	if( argc < 2 ){
		printf("usage: %s idelay_cfg.ini\n", argv[0]);
		return 0;
	}

	char ini_fname[128];
	sprintf(ini_fname, "%s", argv[1]);

	dictionary *ini;
	ini = iniparser_load(ini_fname);
	
	if( ini == NULL ){
		fprintf(stderr, "cannot parse file: %s\n", ini_fname);
		return 0;
	}

	uint reg_vals[REG_NUM] = {0};

//	对CCD290的配置

	char tmp[128];
	int i;
	for( i=0; i<16; i++ ){
		sprintf(tmp,"IDELAY:cntvaluein_%d", i+1);
		reg_vals[i] = (uint)iniparser_getint(ini, tmp, 15);
		printf("reg_vals[%2d] = %d\n", i+1, reg_vals[i]);
	}

    int fd = open("/dev/mem", O_RDWR | O_SYNC);
    uint *ptr = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_SPI_PS_CTRL);
    uint *ctrl_reg = ptr;       // 0x41230000
    uint *data_reg = ptr + 2;   // 0x41230008

    SPI_PS_SEND_DATA_new(   ctrl_reg,
                            data_reg, 
                            reg_vals, 
                            REG_NUM,
                            SPI_PS_IDELAY_CTRL );

    munmap(ptr,10);
    close(fd);

	iniparser_freedict(ini);

	return 0;
}
